﻿/*****************************************************************************
* File			: src.Module
* Purpose		: Contains Declaration and Implementation for Module class
* Creation date : April 24, 2009
* Author		: Sami TOUIL
******************************************************************************/
package src 
{
	// Imports
	import flash.display.MovieClip;
	import flash.events.*
	import flash.ui.*
	import flash.filters.*
	

	/**************************************
	* Class		: Module
	* Purpose	: Logic for Module behavior
	* Parent	: MovieClip
	**************************************/
	public class Module extends MovieClip
	{
		// Class variables
		private			var nMaxHealth		:int	= 50;	// Maximum HP for our Module
		private			var nCurrentHealth	:int;			// HP for our Module
		private			var nPosition		:int;			// Position of the module : 1 is up, -1 is down
		
		// Constructor
		public function Module(nX:int, nY:int, nPos:int)
		{			
			// Set listener for ENTER_FRAME event
			addEventListener(Event.ENTER_FRAME, enterFrame);
			cacheAsBitmap = true;

			// Set coords
			x = nX;
			y = nY;
			
			// Set position
			nPosition = nPos;
			
			// add a dropshadow filter 
			var filter:BitmapFilter = new DropShadowFilter(3,45,0x000000,30,5,5);
           	filters = [filter];
			
			// Add module sprite
			Game.main.spriteClip.addChild(this);
		}

		/***********************************************************
		* Method		: enterFrame
		* Purpose		: frame loop
		* Parameters	: e:Event
		* return		: void
		***********************************************************/
		public function enterFrame(e:Event)
		{
			// if the ship is not deleted
			if(Game.main.ship != null)
			{
				x = Game.main.ship.x;
				y = Game.main.ship.y + 20 * nPosition;
			}

		}
				
		/***********************************************************
		* Method		: onDelete
		* Purpose		: called for deleting the object instance
		* Parameters	: None
		* return		: void
		***********************************************************/
		public function onDelete()
		{
			// Explosion
			new Explosion(x	, y);
			
			//remove this ship from the stage
			Game.main.spriteClip.removeChild(this);
			
			if (this == Game.main.ship.module1)
				Game.main.ship.module1 = null;
				
			if (this == Game.main.ship.module2)
				Game.main.ship.module2 = null;
		}
				
		/***********************************************************
		* Method		: updateHealth
		* Purpose		: Update ship health
		* Parameters	: nPoints:int : The HP we want to add/remove
		* return		: void
		***********************************************************/
		public function updateHealth(nPoints:int)
		{
			nCurrentHealth += nPoints;

			// If HP = 0, delete ship and show Gameovermenu
			if (nCurrentHealth <= 0)
				onDelete();
		}
	}
}